package o;

import android.annotation.TargetApi;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.AsyncTask;
import android.os.Build.VERSION;
import com.huawei.appgallery.foundation.account.bean.UserSession;
import com.huawei.appmarket.service.appmgr.bean.UsageAppInfo;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import o.cep.d;

public class dtc extends AsyncTask<Void, Void, Void> {
    private static final Executor ॱ = Executors.newFixedThreadPool(1);
    private int ˊ;

    protected /* synthetic */ Object doInBackground(Object[] objArr) {
        return ˎ((Void[]) objArr);
    }

    protected /* synthetic */ void onPostExecute(Object obj) {
        ˊ((Void) obj);
    }

    public dtc(int i) {
        this.ˊ = i;
    }

    public static void ˋ(int i) {
        new dtc(i).executeOnExecutor(ॱ, new Void[0]);
    }

    protected Void ˎ(Void... voidArr) {
        if (VERSION.SDK_INT >= 22) {
            if (this.ˊ == 0) {
                if (!dte.ॱ().ˎ()) {
                    dte.ॱ().ˋ(ˋ());
                }
            } else if (UserSession.getInstance().isLoginSuccessful() && UserSession.getInstance().getHomeCountry().equals("CN")) {
                ˏ();
            }
        }
        return null;
    }

    protected void ˊ(Void voidR) {
        super.onPostExecute(voidR);
    }

    @TargetApi(22)
    private List<dsh> ˋ() {
        Logger.ˎ("GetLastUsedAppTask", "GetLastUsedApp list from system function begin");
        UsageStatsManager usageStatsManager = (UsageStatsManager) ContentManager.getInstant().getContext().getSystemService("usagestats");
        if (usageStatsManager == null) {
            Logger.ˋ("GetLastUsedAppTask", "UsageStatsManager is null");
            return null;
        }
        Calendar instance = Calendar.getInstance();
        long timeInMillis = instance.getTimeInMillis();
        instance.add(5, -90);
        List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(2, instance.getTimeInMillis(), timeInMillis);
        List<dsh> arrayList = new ArrayList();
        for (UsageStats usageStats : queryUsageStats) {
            dsh o_dsh = new dsh();
            o_dsh.ˏ(usageStats.getPackageName());
            o_dsh.ˎ(usageStats.getLastTimeUsed());
            arrayList.add(o_dsh);
        }
        dte.ॱ().ˋ((List) arrayList);
        Logger.ˎ("GetLastUsedAppTask", "GetLastUsedApp list from system function end. lastused list size: " + arrayList.size());
        return arrayList;
    }

    @TargetApi(22)
    private void ˏ() {
        Logger.ˎ("GetLastUsedAppTask", "report last used app begin");
        UsageStatsManager usageStatsManager = (UsageStatsManager) ContentManager.getInstant().getContext().getSystemService("usagestats");
        PackageManager packageManager = ContentManager.getInstant().getContext().getPackageManager();
        if (usageStatsManager == null || packageManager == null) {
            Logger.ˋ("GetLastUsedAppTask", "param is null");
            return;
        }
        Calendar instance = Calendar.getInstance();
        long timeInMillis = instance.getTimeInMillis();
        instance.add(2, -6);
        long timeInMillis2 = instance.getTimeInMillis();
        List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(4, timeInMillis2, timeInMillis);
        List arrayList = new ArrayList();
        for (UsageStats usageStats : queryUsageStats) {
            if (arrayList.size() >= 1000) {
                break;
            }
            UsageAppInfo usageAppInfo = new UsageAppInfo();
            String packageName = usageStats.getPackageName();
            long lastTimeUsed = usageStats.getLastTimeUsed();
            usageAppInfo.ˎ(packageName);
            usageAppInfo.ˏ(lastTimeUsed);
            if (lastTimeUsed >= timeInMillis2 && cep.ˎ(packageName) == d.NORMAL) {
                try {
                    PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
                    usageAppInfo.ˋ(packageInfo.versionName);
                    usageAppInfo.ˏ(packageInfo.versionCode);
                    arrayList.add(usageAppInfo);
                } catch (NameNotFoundException e) {
                    Logger.ˋ("GetLastUsedAppTask", "package not found");
                }
            }
        }
        cvt o_dsk = new dsk();
        o_dsk.ˏ(arrayList);
        cfw.ˎ(o_dsk, null);
        Logger.ˎ("GetLastUsedAppTask", "report last used app end");
    }
}
